package com.atguigu.tingshu.user.api;

import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.user.UserListenProcess;
import com.atguigu.tingshu.user.service.UserListenProcessService;
import com.atguigu.tingshu.vo.user.UserListenProcessVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@Tag(name = "用户声音播放进度管理接口")
@RestController
@RequestMapping("api/user")
@SuppressWarnings({"all"})
public class UserListenProcessApiController {

	@Autowired
	private UserListenProcessService userListenProcessService;

	///api/user/userListenProcess/getTrackBreakSecond/{trackId}
	@Operation(summary = "获取声音的上次跳出时间")
	@GetMapping("/userListenProcess/getTrackBreakSecond/{trackId}")
	@GuiGuLogin(required = false)
	public Result<BigDecimal> getTrackBreakSecond(@PathVariable Long trackId){
		Long userId = AuthContextHolder.getUserId();
		if(userId !=null){
			//从DB里面获取用户的最新时间
			 UserListenProcess  userListenProcess = userListenProcessService.getTrackBreakSecond(trackId,userId);
			 if(userListenProcess!=null){
				 return Result.ok(userListenProcess.getBreakSecond());
			 }

		}
		return Result.ok(new BigDecimal("0"));
	}

	///api/user/userListenProcess/updateListenProcess
	@Operation(summary = "更新播放进度")
	@PostMapping("/userListenProcess/updateListenProcess")
	@GuiGuLogin(required = false)
	public Result updateListenProcess(@RequestBody UserListenProcessVo userListenProcessVo){
		Long userId = AuthContextHolder.getUserId();
		if(userId !=null){
			//更新DB时间
			 userListenProcessService.updateListenProcess(userId ,userListenProcessVo);
		}
		System.out.println("updateListenProcess -> userId = " + userId);
		return Result.ok();
	}

}

